<?php
namespace app\index\model;
use think\Db;
use think\Model;
use think\Exception;

class Report extends Model
{
    protected $table = 'lis_ybxx';

    //返回查询的样本信息
    public function GetReportInfoByYbid($str){
        $data = [];
        return $this->where('ybid',$str)->order('cjtime desc')->select();
    }

    //返回查询的样本信息 --根据 ybid 及 yqdh
    public function GetReportInfoByYbidAndYqdh($str,$ybbh,$cdrq){
        $data = ['ybid'=>$str,'ybbh'=>$ybbh,'cdrq'=>$cdrq];
        return $this->where($data)->select();
    }


    //返回查询的信息  根据姓名
    public function GetReportInfoByBrxm($str){
        $data = [];
        return $this->where('brxm',$str)->order('cjtime desc')->select();
    }

    //返回医院条码查询的数据

    public function GetReportInfoByYytm($str){
        $data = [];
        return $this->where('yytm',$str)->order('cjtime desc')->select();
    }
    //返回身份证号码查询的数据

    public function GetReportInfoByIdNumber($str){
        $data = [];
        return $this->where('idnumber',$str)->order('cjtime desc')->select();
    }

    //根据条码，id来快速修改身份证
    public  function ModifyIdnumber($id,$idnumber){
        //先修改f_k_ybxx  lis_ybxx
        $fkres = Db::table('f_k_ybxx')->where('ybid',$id)->update(['idNumber' =>$idnumber]);
        $ybres = Db::table('Lis_ybxx')->where('ybid',$id)->update(['idnumber' =>$idnumber]);
        //这里特别注意 idNumber字段的大小区别
        if($fkres &&  $ybres){
            return true;
        }
        return false;
    }

    //根据传入的yqdh  cdrq ybbh  $xmbh  $xmdcz来修改 更新报告
    public function ModifyReportByYCY($yqdh, $cdrq, $ybbh, $xmdh, $data)
    {
        $datas = ['yqdh' => $yqdh, 'cdrq' => $cdrq, 'ybbh' => $ybbh, 'xmdh' => $xmdh];
        $res = Db::table('Lis_xmcdz')->where($datas)->update($data);
        if ($res) {
            return true;
        }
        return false;
    }

    //根据传入的yqdh  cdrq ybbh  $xmbh 删除结果
    public function DelReportByYCY($yqdh, $cdrq, $ybbh, $xmdh)
    {
        $data = ['yqdh' => $yqdh, 'cdrq' => $cdrq, 'ybbh' => $ybbh, 'xmdh' => $xmdh];
        $res = Db::table('Lis_xmcdz')->where($data)->delete();
        if($res){
            return true;
        }
        return false;
    }

    //根据传入的yqdh  cdrq ybbh 来查询报告 修改结果  xmcdz
    public function GetReportByYCY($yqdh,$cdrq,$ybbh){
        /*$data = ['yqdh' =>$yqdh,'cdrq'=>$cdrq,'ybbh'=>$ybbh ];*/
        return $res = Db::query("select * from lis_xmcdz where yqdh = :yqdh and cdrq = :cdrq and ybbh = :ybbh",
            ['yqdh' =>$yqdh,'cdrq' =>$cdrq ,'ybbh'=>$ybbh]);
        }


    //根据仪器平台，检测项目  统计每天的标本量数据
    public function GetCountDay($yqdh = '', $jymd)
    {
        return $res = Db::query("SELECT cdrq,COUNT(cdrq) AS 数量 FROM lis_ybxx WHERE yqdh = :yqdh AND jymd = :jymd GROUP BY cdrq ORDER BY cdrq ASC", ['yqdh' => $yqdh, 'jymd' => $jymd]);
    }


    //导入的时候 根据 ybid   $columnname列名  值    来更新对应的字段信息

    public function UpdateImportByYbid($ybid, $str, $columnname)
    {
        /*if (strlen($ybid) !== 12) {
            return false;
        }*/
        $data = ['ybid' => $ybid];
        if ($columnname == "yytm") {
            //更新医院条码
            return $res = Db::table('lis_ybxx')->where($data)->update(['yytm' => $str]);
        } elseif ($columnname == "brxm") {
            return $res = Db::table('lis_ybxx')->where($data)->update(['brxm' => $str]);
        } elseif ($columnname == "beizhu") {
            return $res = Db::table('lis_ybxx')->where($data)->update(['beizhu' => $str]);
        } elseif ($columnname == "idnumber") {
            return $res = Db::table('lis_ybxx')->where($data)->update(['idnumber' => $str]);
        } elseif ($columnname == "cjtime") {
            return $res = Db::table('lis_ybxx')->where($data)->update(['cjtime' => $str]);
        } elseif ($columnname == "cyrq") {
            return $res = Db::table('lis_ybxx')->where($data)->update(['cyrq' => $str]);
        } elseif ($columnname == "bgrq") {
            return $res = Db::table('lis_ybxx')->where($data)->update(['bgrq' => $str]);
        }elseif ($columnname == "brnl") {
            return $res = Db::table('lis_ybxx')->where($data)->update(['brnl' => $str]);
        }
        return false;
    }

}